/**
 * Created by Administrator on 2017/4/26.
 */
app.controller('commenCtr',
    ['$scope','$location','$routeParams','$http','$sce','$rootScope', function ($scope,$location,$routeParams,$http,$sce,$rootScope) {
/***
 * 普通稿件评论
 * */
    var articleIdStrin =$routeParams.fileId;
    console.log("aritString")
    console.log(articleIdStrin)
    var topicIndex= articleIdStrin.indexOf("topic");
    var topic=articleIdStrin.slice(topicIndex+5,articleIdStrin.length);
        console.log("topic")
        console.log(topic)
    var sourcIndex=articleIdStrin.indexOf("type");
        // 评论类型 文章专题直播视频
    var source=articleIdStrin.slice(sourcIndex+4,sourcIndex+5);
        console.log("source类型")
        console.log(source)
    // 评论文章的id
     var articleId =articleIdStrin.slice(0,sourcIndex);
        console.log("articleId")
        console.log(articleId)
        // var articleId=174;
        //获取最新评论  page=0开始;加载更多评论
        var compage=0;
        //加载更多最新评论（只有最新评论有分页）id=3969全替换为articleId*****
        $scope.LoadMoreCom=function () {
                compage++;
                $http.jsonp(newComment+'id='+articleId+'&siteId=1&lastFileId=0&page='+compage+'&source='+source+'&jsoncallback=JSON_CALLBACK'
                ).success(function(data,header,config,status){
                    // 是否还有更多评论
                    $scope.hasMorCom=data.hasMore;
                    $scope.commenCon=$scope.commenCon.concat(data.list);
                }).error(function(data,header,config,status){

                });

        };

        //获取最新评论
        $scope.getNewCom=function () {
            $scope.commeInputs=siteConfig.defaultHint;
            console.log(compage);
            $http.jsonp(newComment+'id='+articleId+'&siteId=1&lastFileId=0&page=0&source='+source+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("第一次");
                console.log(data);
                // 是否还有更多评论
                $scope.hasMorCom=data.hasMore;
                $scope.commenCon=data.list;
            }).error(function(data,header,config,status){

            });
        };

        //// 加载详情页时先加载最新评论一次，顺序问题,不能放getNewCom方法前
        //$scope.getNewCom();
        //获取 热门评论 id = '+articleId+'??
        $scope.getHotCom=function () {
            $http.jsonp(hotComment+'id='+articleId+'&siteId=1&source='+source+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log(data);
                // 是否还有更多评论
                $scope.hasMorCom=data.hasMore;
                //获取评论
                $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        };

        //获取全局配置信息
        $scope.getCommentInfo=function(){
            $http.jsonp(commInfo+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("全局评论信息");
                console.log(data);
                $scope.siteConfig=data.siteConfig.discuss;
                //评论框提示语
                siteConfig.defaultHint=$scope.siteConfig.defaultHint;
                //官方评论用户名
                siteConfig.defaultName=$scope.siteConfig.defaultName;
                $scope.defaultName=$scope.siteConfig.defaultName;
                //官方评论头像
                siteConfig.defaultIcon=$scope.siteConfig.defaultIcon;
                $scope.siteConfigDefaultIcon=$scope.siteConfig.defaultIcon;
                console.log("官方用户名");
                console.log($scope.defaultName);
                //是否关闭全站评论功能
                $scope.siteConfigAuditType=$scope.siteConfig.auditType;
                //全局评论颜色问题
                $scope.commStyleColor=$scope.siteConfig.styleColor;
                commentStyle=$scope.commStyleColor;
                $rootScope.changeComStyle();

                //获取最新和热门评论
                $scope.getNewCom();
            }).error(function(data,header,config,status){

            });

        };
        $scope.getCommentInfo();


        //弹出模态框
        $scope.showModeAndRepley=function ($event) {
                $('#commentModle').css('display',"block");
                $("#commentArea").css('display',"block");
                $("#comInput").focus();
                $("#comm_text_footer").css('display',"none");
            $scope.parentId=0;
        };
        //取消模态框
        $scope.cancelCom=function () {
            $('#commentModle').css('display',"none");
            $("#commentArea").css('display',"none");
            $("#comm_text_footer").css('display',"block");
        }
        //回复评论
        $scope.replayCom=function (parentId,userName,ParentUserID) {
            $('#commentModle').css('display',"block");
            $("#commentArea").css('display',"block");
            $("#comm_text_footer").css('display',"none");
            // 提交评论使用
            $scope.parentId=parentId;
            $scope.userName=userName;
            $scope.ParentUserID=ParentUserID;
            $("#comInput").attr("placeholder","@"+userName+":");
        };

        // 发送评论
        // 发送评论
        $scope.sendCom=function () {
            var commentImgUrl=$("#file")[0].files[0];
            console.log("files")
            if(commentImgUrl){
                console.log($("#file")[0].files[0])
                if(commentImgUrl){
                    var imgUrl=commentImgUrl;
                }
                //评论上传图片??
                console.log("评论图片")
                console.log($('#commentImgFile').val());


                //图片提交


                var formdata = new FormData();
                formdata.append("Filedata", imgUrl);
                formdata.append("siteID", 1);
                formdata.append("location", 0);
                formdata.append("userId", "0");
                formdata.append("fileType", "picture");
                formdata.append("uniqid", "B715C9A0-D580-46F2-ABD1-F244EA24C934");

                $.ajax({
                    type:'POST',
                    url: commUpload,
                    processData: false,
                    data: formdata,
                    async:false,
                    contentType: false,    //这个一定要写
                    dataType:'json',
                    success: function(result){
                        //成功后处理代码
                        //self.onSuccess(file, result);
                        //if(self.curUploadFile.length==0){
                        // 回调全部完成方法
                        //self.onComplete("全部完成");
                        console.log("上传成功");
                        console.log(result)
                        console.log("上传成功url");
                        var comimgUrl=JSON.parse(result).fileList[0].url;
                        console.log(comimgUrl);


                        setTimeout(function(){
                            //评论成功之后提交回复
                            var sendMesg=$("#comInput").val();
                            console.log("评论信息");
                            console.log(sendMesg);
                            var logUserId=uid_fouNder;
                            var LogUserName=username_fouNder;
                            //匿名评论
                            if(!uid_fouNder){
                                logUserId=0;
                                LogUserName="触屏用户";
                            }
                            var topicTitle= $scope.detailData.title;
                            var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                                parentUserID = $scope.ParentUserID, topic =topicTitle, userID=logUserId
                                , userName=LogUserName, type = 0,content=sendMesg;

                            //提交回复
                            $.ajax({
                                type: "post",
                                url: sentComent,
                                data:{
                                    "siteID": siteID,
                                    "rootID": articleId,
                                    "parentID": parentID,
                                    "parentUser": parentUser,
                                    "parentUserID": parentUserID,
                                    "topic": topic,
                                    "userID": userID,
                                    "userName": userName,
                                    "type": type,
                                    "content":content,
                                    "channel":1,
                                    "sourceType":artSource,
                                    "imgUrl":comimgUrl

                                },
                                dataType : "json",
                                //jsonp: "jsoncallback",
                                //jsoncallback:"jsoncallback",
                                success: function(data){
                                    if(data){
                                        console.log(data);
                                        alert("评论成功！");
                                        $scope.getNewCom();
                                        console.log("发送内容为：");
                                        console.log(sendMesg);
                                        $("#comInput").val("");
                                        $('#commentModle').css('display',"none");
                                        $("#commentArea").css('display',"none");
                                        $("#comm_text_footer").css('display',"block");
                                        $('.up-section').each(function(el,value){
                                            console.log("图片数组")
                                            alert()
                                            console.log(el)
                                            console.log(value)
                                            //arrFiles=[]
                                            //imgArr=[]
                                            loadImg();
                                        });
                                    }else{
                                        alert("评论失败！1");
                                    }

                                },
                                error: function(){
                                    alert("评论失败！2");
                                    $('#commentModle').css('display',"none");
                                    $("#commentArea").css('display',"none");
                                    $("#comm_text_footer").css('display',"block");
                                }
                            });
                        },2000)

                        //}
                    },
                    error: function(){
                        self.onFailure(file);
                    }

                });
                //只提交评论
            }else{
                //评论成功之后提交回复
                var sendMesg=$("#comInput").val();
                console.log("评论信息");
                console.log(sendMesg);
                var logUserId=uid_fouNder;
                var LogUserName=username_fouNder;
                //匿名评论
                if(!uid_fouNder){
                    logUserId=0;
                    LogUserName="触屏用户";
                }
                var topicTitle= $scope.detailData.title;
                var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                    parentUserID = $scope.ParentUserID, topic =topicTitle, userID=logUserId
                    , userName=LogUserName, type = 0,content=sendMesg;

                //提交回复
                $.ajax({
                    type: "post",
                    url: sentComent,
                    data:{
                        "siteID": siteID,
                        "rootID": articleId,
                        "parentID": parentID,
                        "parentUser": parentUser,
                        "parentUserID": parentUserID,
                        "topic": topic,
                        "userID": userID,
                        "userName": userName,
                        "type": type,
                        "content":content,
                        "channel":1,
                        "sourceType":artSource

                    },
                    dataType : "json",
                    //jsonp: "jsoncallback",
                    //jsoncallback:"jsoncallback",
                    success: function(data){
                        if(data){
                            console.log(data);
                            alert("评论成功！");
                            $scope.getNewCom();
                            console.log("发送内容为：");
                            console.log(sendMesg);
                            $("#comInput").val("");
                            $('#commentModle').css('display',"none");
                            $("#commentArea").css('display',"none");
                            $("#comm_text_footer").css('display',"block");
                            $('.up-section').each(function(el,value){
                                console.log("图片数组")
                                alert()
                                console.log(el)
                                console.log(value)
                                //arrFiles=[]
                                //imgArr=[]
                                loadImg();
                            });
                        }else{
                            alert("评论失败！1");
                        }

                    },
                    error: function(){
                        alert("评论失败！2");
                        $('#commentModle').css('display',"none");
                        $("#commentArea").css('display',"none");
                        $("#comm_text_footer").css('display',"block");
                    }
                });
            }
        };


        //回到顶部
        $(".fiBtn").hide();
        $(window).on("scroll", function(){
            if($(window).scrollTop() >0){
                $(".fiBtn").show();
            }else{
                $(".fiBtn").hide();
            }
        });
        $scope.goBackTop=function () {
            $("body").animate({scrollTop: 0}, 300);
        };

        $scope.lunchaMorChilCom=true;
        //获取子评论加载更多更多内容 id为每条父评论的id号
        $scope.checkMorChildCom=function (id,index) {
            $scope.lunchaMorChilCom=false;

            $http.jsonp(replayComent+'id='+id+'&page=0&siteId=1&source=0&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                //点击查看更多子评论之前
                $scope.beforLoadMorChid=$scope.commenCon[index].topDiscuss.list;
                //点击查看更多子评论之后
                $scope.commenCon[index].topDiscuss.list=data.list;
                // 是否还有更多评论
               /*console.log("是否有更多")
                console.log(data.hasMore)*/
                $scope.hasNoChildMorCom={};
                $scope.hasChildMorCom={};
                 if(data.hasMore){
                     $scope.hasChildMorCom.index=true;
                 }else{
                     $scope.hasNoChildMorCom.index=true;

                 }
                //获取评论
                // $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        }

}])
